package com.lw.extraceim.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;
import com.lw.extraceim.app.ExtraceApplication;
import com.lw.extraceim.loader.RouteLoader;
import com.lw.extraceim.net.HttpResponseParam;
import com.lw.extraceim.net.ResponseCode;
import com.lw.extraceim.net.callback.HttpResponseParamCallback;
import com.lw.extraceim.util.MapUtils;
import com.lw.extraceim.view.ExtraceToolbar;

import java.util.ArrayList;
import java.util.Date;

import okhttp3.Call;

public class MapService extends Service {

    private static final String TAG = "MapService";
    //先前的坐标点
    private LatLng preLatlng = null;
    private LocationClient locationClient = null;
    //获得的坐标点集合
    private ArrayList<LatLng> latLngs = new ArrayList<>();

    private RouteLoader loader;
    protected ExtraceApplication application;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public void onCreate() {
//        getApplication(
        //Toast.makeText(getApplicationContext(),"绑定服务",Toast.LENGTH_SHORT).show();
        loader = new RouteLoader(this);
        application = (ExtraceApplication) getApplication();

        Log.d("MapService", "绑定服务");
        locationClient = new LocationClient(this);
        LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");
        option.setOpenGps(true);
        option.setPriority(LocationClientOption.NetWorkFirst);
        option.setProdName("MyBaiduMap");
        option.setScanSpan(3000); //获取位置信息时间
        locationClient.setLocOption(option);
        locationClient.registerLocationListener(new MyLocationListener());
        locationClient.start();
        locationClient.requestLocation();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MapService", "解绑服务");
        if (locationClient != null && locationClient.isStarted()) {
            locationClient.stop();
            locationClient = null;
        }
    }

    private class MyLocationListener extends BDAbstractLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (locationClient == null)
                return;
            if (preLatlng == null) {
                //位置信息 第一个点
                preLatlng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
            } else if(bdLocation.getLatitude() > 0.1 && bdLocation.getLongitude() > 0.1) {
                LatLng end = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                double disance = MapUtils.getDistance(preLatlng, end);
                Log.d(TAG, "onReceiveLocation: " + end.latitude + "-" + end.longitude);
//                Toast.makeText(getApplicationContext(), disance + "米", Toast.LENGTH_SHORT).show();
                if (disance >= 3.0f) {
                    //当前位置与上一位置相差三米时上传位置信息
                    preLatlng = end;
                    uploadRoute(end.latitude,end.longitude);
                }
            }
        }
    }

    /**
     * 上传位置信息
     *
     * @param x
     * @param y
     */
    public void uploadRoute(double x, double y) {
        loader.upload(application.getUserInfo().getuId(), x, y, new HttpResponseParamCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.d(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onResponse(HttpResponseParam response, int id) {
                Log.d(TAG, "onResponse: " + response.reseponseJson);
                if (response.code.getCode() == ResponseCode.SUCESS) {
                    Log.d(TAG, "onResponse: 上传成功");
                }
            }
        });
    }

    public void checkIsTransporting(){
        //判断用户是否正在运送包裹2
    }

    private ArrayList<LatLng> getLatLngs() {
        return this.latLngs;
    }

    public class MyBinder extends Binder {
        public ArrayList<LatLng> getLatLngs() {
            return MapService.this.getLatLngs();
        }
    }
}
